package com.zkingsoft.actions.admin;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zkingsoft.actions.common.BaseController;
import com.zkingsoft.anotations.RemoveRequestToken;
import com.zkingsoft.anotations.SaveRequestToken;
import com.zkingsoft.constance.Dictionary;
import com.zkingsoft.model.Article;
import com.zkingsoft.plugin.pojo.PaginationVO;
import com.zkingsoft.plugin.util.WebUtil;
import com.zkingsoft.pojo.AjaxResult;
import com.zkingsoft.services.ArticleService;

/**
 * @author dingchuan
 * @date 2016-07-15 11:19
 * 文章Controller
 */
@Controller
@RequestMapping(value = "admin/article")
public class ArticleController extends BaseController{

	@Resource
	private ArticleService currentService;
	
	/**
	 * 列表显示
	 */
	@RequestMapping(value = "/showList")
	public @ResponseBody AjaxResult showList(Article article, PaginationVO pageVo) {
		return showList(currentService, article, pageVo);
	}
   	
	/**
	 * 新增或者修改页面
	 */   	
   	@RequestMapping(value = "/addOrModify")
   	@RemoveRequestToken
	public @ResponseBody AjaxResult addOrModify(Article article) {
		if (article.getId() != null) {
			return modify(currentService, article, "文章");
		} else {
			article.setCreatetiem(new Date());
			return add(currentService, article, "文章");
		}
	}
	
   	/**
	 * 进入修改界面
	 */   
   	@RequestMapping(value = "/editForm")
   	@SaveRequestToken
	public String editForm(Long id) {
		Article article;
		if (id != null) {
			article = currentService.findById(id);
			WebUtil.getRequest().setAttribute("obj", article);
		}
		return "admin/articleManage-form";
	}
   	
   	
   	/**
	 * 删除
	 */  
 	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(String keys) {
		return remove(currentService, keys);
	}
 	
 	/**
 	 * 改变发布状态：已发布
 	 * @param id
 	 * 
 	 */
 	@RequestMapping(value = "/setArticleStaticY")
 	public @ResponseBody AjaxResult setArticleStaticY(Long id) {	
 		Article article = currentService.findById(id);
 		article.setIsPublish(Dictionary.FLAG_YES);
		return modify(currentService, article, "发布状态");
	}
 	    
 	/**
 	 * 改变发布状态：未发布
 	 * @param id
 	 * 
 	 */
 	@RequestMapping(value = "/setArticleStaticN")
 	public @ResponseBody AjaxResult setArticleStaticN(Long id) {	
 		Article article = currentService.findById(id);
 		article.setIsPublish(Dictionary.FLAG_NO);
		return modify(currentService, article, "发布状态");
	}
 	
}